package com.chukun.netty.chat;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class NettyChatClient {

    private static final int PORT=8899;
    private void start() throws Exception{
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try{
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new NettyChartClientChannelInitializer());
            Channel channel = bootstrap.connect("127.0.0.1",PORT).sync().channel();
            BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
            for (;;){
                channel.writeAndFlush(bf.readLine()+"\n");
            }
        }finally {
            workGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new NettyChatClient().start();
    }
}
